package com.zenithmind.library.pojo.domain;

import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.zenithmind.common.base.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * 图书基本信息实体
 * 遵循单一职责原则：只负责图书基本信息的存储
 *
 * @author ZenithMind
 * @since 2024-06-14
 */
@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Schema(name = "图书实体", description = "图书基本信息")
@TableName("lib_book")
public class Book extends BaseEntity {

    @NotBlank(message = "图书标题不能为空")
    @Size(max = 500, message = "图书标题长度不能超过500个字符")
    @Schema(description = "图书标题")
    private String title;

    @Size(max = 500, message = "副标题长度不能超过500个字符")
    @Schema(description = "副标题")
    private String subtitle;

    @NotBlank(message = "ISBN不能为空")
    @Size(max = 20, message = "ISBN长度不能超过20个字符")
    @Schema(description = "ISBN")
    private String isbn;

    @Size(max = 20, message = "ISSN长度不能超过20个字符")
    @Schema(description = "ISSN")
    private String issn;

    @NotNull(message = "分类ID不能为空")
    @Schema(description = "分类ID")
    private String categoryId;

    @Schema(description = "出版社ID")
    private String publisherId;

    @Schema(description = "出版日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate publishDate;

    @Schema(description = "版本")
    private String edition;

    @Schema(description = "页数")
    private Integer pages;

    @Schema(description = "语言")
    @Size(max = 50, message = "语言长度不能超过50个字符")
    private String language;

    @Schema(description = "图书简介")
    @Size(max = 5000, message = "图书简介长度不能超过5000个字符")
    private String summary;

    @Schema(description = "目录")
    @Size(max = 10000, message = "目录长度不能超过10000个字符")
    private String tableOfContents;

    @Schema(description = "封面图片URL")
    private String coverImage;

    @Schema(description = "价格")
    private BigDecimal price;

    @Schema(description = "关键词，多个用逗号分隔")
    private String keywords;

    @Schema(description = "标签，多个用逗号分隔")
    private String tags;

    @Schema(description = "图书类型 (0-纸质书, 1-电子书, 2-音频书)")
    private Integer bookType;

    @Schema(description = "状态 (0-下架, 1-上架)")
    private Integer status;

    @Schema(description = "是否推荐 (0-否, 1-是)")
    private Integer isRecommended;

    @Schema(description = "是否新书 (0-否, 1-是)")
    private Integer isNew;

    @Schema(description = "是否热门 (0-否, 1-是)")
    private Integer isPopular;

    @Schema(description = "位置信息")
    private String location;

    @Schema(description = "备注")
    private String remarks;

    /**
     * 获取完整标题（包含副标题）
     */
    public String getFullTitle() {
        if (subtitle != null && !subtitle.trim().isEmpty()) {
            return title + " - " + subtitle;
        }
        return title;
    }

    /**
     * 判断是否为电子书
     */
    public boolean isEBook() {
        return bookType != null && bookType == 1;
    }

    /**
     * 判断是否为音频书
     */
    public boolean isAudioBook() {
        return bookType != null && bookType == 2;
    }

    /**
     * 判断是否已上架
     */
    public boolean isPublished() {
        return status != null && status == 1;
    }

    /**
     * 判断是否为推荐图书
     */
    public boolean isRecommendedBook() {
        return isRecommended != null && isRecommended == 1;
    }
}
